<script>
$('#Mobile').blur(function(){
	if($(this).val() == '') {
		return false;	
	}
	$.post('{{urlfor "ArchivesController.SelectArchives"}}', {"keyword":$(this).val()}, function(j){
		if (j.code == 1) {
			searcharchives()	
		}
	}, 'json')  
});

$('#Age').blur(function(){
	var myDate = new Date();
	var value = $(this).val();
	if(value == '') {
		value = 0;	
	}	
	full_year = myDate.getFullYear();
	month = myDate.getMonth()+1;
	day = myDate.getDate();
	
	cut_year = full_year - value;
	if($('#Birthday').val() == ''){
		$('#Birthday').val(cut_year+'-'+month+'-'+day);
	}
});


function receiveFormData(d) {
	if(!d) return false;
	
	for (var key in d){
		 val = d[key]
		 if(key == "Sex") {
			 $('input[name="Sex"]').each(function(){ if($(this).attr("value") == val) { $(this).prop("checked", true) }  });
		 }else if(key =="address") {
			 //address = val.split("-")
			 //addressInit('cmbProvince', 'cmbCity', 'cmbArea',"", (address[0]?address[0]:''), (address[1]?address[1]:''), (address[2]?address[2]:''));
		 }else{
			$("#"+key).val(val)
		 }
	 }
	 $('select').trigger('changed.selected.amui');
}

$('#SchoolId').on('change', function(){
	$('#SchoolName').val($(this).find(':selected').text())
});

$(function(){
	$selected = $('#SchoolId');
	$selected.val({{.m.SchoolId}})
	
	$('select').trigger('changed.selected.amui');
	
	$('input[value="{{.m.Sex}}"]').prop('checked', true);

    getWechatFollows();
});

$('#reload_follows').click(function(){
    getWechatFollows();
});

function getWechatFollows() {
    $.post('{{ urlfor "ArchivesController.GetWechatFollows" }}', function(j){
        if(j.data) {
            var html = '';
            $.each(j.data, function (index, value) {
                html += '<li class="am-cf" data-name="'+ value.NickName +'" data-openid="'+ value.Openid +'" data-sex="'+ value.Sex +'" data-address="'+ value.Address +'">\n' +
                    '<i><img src="'+ value.Headimg +'"></i>\n' +
                    '<dd>'+ value.NickName +'<br />'+ value.Sex +' &nbsp;&nbsp;&nbsp; '+ value.Address +'</dd>\n' +
                    '</li>';
            });
            html != '' && $('#follows').html(html);
            setBindEvent();
        }
    }, 'json');
    return false;
}

//设置绑定事件
function setBindEvent() {
    $('#follows li').click(function(){
        var name = $(this).data('name');
        var openid = $(this).data('openid');
        var sex = $(this).data('sex');
        var address = $(this).data('address');
        var id = $('#Id').val();
        $('#Openid').val(openid);
        if(id == 0) {
            $('#Realname').val(name);
            $('#Address').val(address);
            $('[name="Sex"]').each(function () {
                value = $(this).val();
                if(value == sex) {
                    $(this).prop('checked', true);
                } else {
                    $(this).prop('checked', false);
                }
            });
        }
    });
}
$('#Birthday').on('change', function(e){
     $('#Age').val(jsGetAge($(this).val()));
});

/*根据出生日期算出年龄*/
function jsGetAge(strBirthday) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];

    d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();

    if (nowYear == birthYear) {
        returnAge = 0;//同年 则为0岁
    } else {
        var ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
            if (nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay;//日之差
                if (dayDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            } else {
                var monthDiff = nowMonth - birthMonth;//月之差
                if (monthDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            }
        } else {
            returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
        }
    }

    return returnAge; //返回周岁年龄
}

$("#formDoSubmitDialogMy").validator({
    submit: function() {
        if(this.isFormValid()) {
            var that = $("#formDoSubmitDialogMy");
            var submitBtn = that.find('button[type="submit"]');
            if(submitBtn.hasClass('am-disabled')){
                return false;
            }
            submitBtn.addClass('am-disabled');
            $.ajax({
                url: that.attr("action")
                , type: 'post'
                , data: that.serialize()
                , dataType: 'json'
                , success: function (response) {
                    layer.msg(response.msg);
                    if (response.code === 0) {
                        setTimeout(function () {
                            submitBtn.removeClass('am-disabled');
                            if(typeof parent.dialogReback == 'function') {
                                parent.dialogReback(response.data);
                            }
                            parent.layer.closeAll('iframe');
                        }, 1000);
                    }else {
                        submitBtn.removeClass('am-disabled');
                    }
                }
                , error: function (error) {
                    submitBtn.removeClass('am-disabled');
                    layer.msg('数据提交失败！');
                }
            });
        }
        return false;
    }
});
</script>